﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Calhoon.MarkupParser
{
	public class Tag
	{
		private string mTagName;
		public string TagName {
			get {
				return mTagName;
			}
			set {
				mTagName = value;
			}
		}

		private Dictionary<string, AttributeValue> mAttributes;
		public Dictionary<string, AttributeValue> Attributes {
			get {
				return mAttributes;
			}
		}

		public Tag() {
			mAttributes = new Dictionary<string, AttributeValue>(StringComparer.OrdinalIgnoreCase);
		}

		protected virtual string RenderAttributes() {
			StringBuilder atts = new StringBuilder();
			foreach (KeyValuePair<string, AttributeValue> att in Attributes) {
				atts.AppendFormat(" {0}={1} ", att.Key, att.Value.RenderAttribute());
			}

			return atts.ToString();
		}

		public override bool Equals(object obj) {
			Tag otherTag = obj as Tag;
			if (obj == null) {
				return false;
			}

			if (this.TagName != otherTag.TagName) {
				return false;
			}

			if (this.Attributes.Count != otherTag.Attributes.Count) {
				return false;
			}

			foreach (KeyValuePair<string, AttributeValue> attribute in this.mAttributes) {
				if (!otherTag.Attributes.ContainsKey(attribute.Key)) {
					return false;
				}

				if (!attribute.Value.Equals(otherTag.Attributes[attribute.Key])) {
					return false;
				}
			}

			return true;
		}

		public override int GetHashCode() {
			return mTagName.GetHashCode() ^ mAttributes.GetHashCode();
		}
	}
}